<?php

class driver_details
{
	var $mode;
	var $data;
		
	var $teams = '';
	
	var $items = Array();

	function driver_details($data, $mode)
	{
		$this->data = $data;
		$this->mode = $mode;
		
		if($mode == 'Mini')
		{
			$this->setupMini();
		}
		else if($mode == 'information')
		{
			$this->setupInformation();
		}
		else if($mode == 'statistics')
		{
			$this->setupStats();
		}
		else if($mode == 'career')
		{
			$this->setupHistory();
		}
		else if($mode == 'incidents')
		{
			include_once('includes/incident_public.php');
			
			$this->items[] =  new incident_public_driver($this->data['user_id']);
		}
		else if($mode == 'license')
		{
			include_once('includes/license_details.php');
			
			$this->items[] =  new license_driver($this->data['user_id']);
		}
		
	}
	function render()
	{
		foreach($this->items as $value)
		{
			$value->render();
		}
		
		if($this->mode == 'Mini')
		{
			$this->RenderMini();
		}
		else if($this->mode == 'information')
		{
			$this->renderInformation();
		}
		else if($this->mode == 'statistics')
		{
			$this->renderStats();
		}
	}
		
	function setupMini()
	{
		global $lang;
		
		$this->teams = '';
		if($this->data['user_teams'] != '')
		{
			$this->teams = '(' . StripSlashes($this->data['user_teams']) . ')';
		}
	}
	
	function setupInformation()
	{
		global $lang, $db, $forum_avatar_dir, $www_forums;
		
		$this->teams = '';
		$q = 'SELECT * FROM ' . TEAMS_TABLE . ', ' . TEAMMEMBERS_TABLE . ' WHERE tm_team = t_id AND tm_approved = 1 and tm_driver = ' .
					 $this->data['user_id'];
		$res = $db->sql_query($q);
		while ($row = $db->sql_fetchrow($res))
		{
			$this->teams .= '<a href="teams.php?id=' . $row['t_id'] . '">' . $row['t_name'] . '</a><br>';
		}
		
		$this->data['user_avatar'] = decodeAvatar($this->data['user_avatar'], $this->data['user_avatar_type']);
		
		$this->data['user_pm'] = $www_forums . 'ucp.php?i=pm&mode=compose&u=' . $this->data['user_id'];
		$this->data['user_profile'] = $www_forums . 'memberlist.php?mode=viewprofile&u=' . $this->data['user_id'];
		
		include_once('includes/bbcode.php');
				
		$this->data['user_sig'] = nl2br(bbencode_second_pass($this->data['user_sig'], $this->data['user_sig_bbcode_uid']));
	}
	
	function setupStats()
	{
		global $lang, $db, $forum_avatar_dir, $www_forums;
		
		$this->teams = '';
		
		$q = 'SELECT (SELECT COUNT(*) FROM lc_raceresults l WHERE r_driver = ' . $this->data['user_id'] . ') as races,
			         (SELECT COUNT(*) FROM lc_raceresults l WHERE r_driver = ' . $this->data['user_id'] . ' and r_raceposition = 1) as wins,
			         (SELECT COUNT(*) FROM lc_raceresults l WHERE r_driver = ' . $this->data['user_id'] . ' and r_raceposition <= 3) as podiums,
				     (SELECT COUNT(*) FROM lc_raceresults l WHERE r_driver = ' . $this->data['user_id'] . ' and r_points > 0) as pts_finishes,
			         (SELECT SUM(r_racelaps) FROM lc_raceresults l WHERE r_driver = ' . $this->data['user_id'] . ') as race_laps,
   				     (SELECT SUM(r_lapsled) FROM lc_raceresults l WHERE r_driver = ' . $this->data['user_id'] . ') as race_led,
			         (SELECT SUM(r_points) FROM lc_raceresults l WHERE r_driver = ' . $this->data['user_id'] . ') as points,
			         (SELECT COUNT(*) FROM lc_raceresults l WHERE r_driver = ' . $this->data['user_id'] . ' and r_qualifyingtime > 0 
					 						and r_qualifyingrank = 1) as pole_positions,
			         (SELECT COUNT(*) FROM lc_raceresults l WHERE r_driver = ' . $this->data['user_id'] . ' 
					 						and r_fastestlap > 0 and r_fastestlap_rank = 1) as fastest_laps';
		$res = $db->sql_query($q);
		if ($row = $db->sql_fetchrow($res))
		{
			$this->data['stats'] = $row;
		}
		
		$this->data['user_avatar'] = decodeAvatar($this->data['user_avatar'], $this->data['user_avatar_type']);
		


	}
	
	function setupHistory()
	{
		global $lang, $db;
		include_once('includes/addMessage.php');
		include_once('includes/driver_career.php');
		include_once('includes/renderInclude.php');
		
		
		$this->teams = '';
		$q = 'SELECT c.c_title AS champ_title, 
					 c.c_id AS champ_id, 
					 c.c_logoThmbURL AS champ_logo, 
					 u.user_id, 
					 pf_drivername as user_drivername, 
					 cs_image, 
					 cs_imageThmb, 
					 ca.c_id AS car_id, 
					 ca.c_model AS car_model, 
					 ca.c_manufacturer AS car_manufacturer, 
					 e_id, 
					 e_title, 
					 ta.t_fullTitle as track_name, 
					 ta.t_location AS track_location, 
					 d_title, 
					 cs_no, 
					 (SELECT t.t_id FROM ' . TEAMS_TABLE . ' t WHERE t.t_id = cs_team LIMIT 0, 1) AS team_id,
					 (SELECT t.t_name FROM ' . TEAMS_TABLE . ' t WHERE t.t_id = cs_team LIMIT 0, 1) AS team_name,
					 date_format(ed_date, "%b %e, %Y") as event_date 
				FROM ' . EVENTSIGNUPS_TABLE . ', ' . CHAMPSIGNUPS_TABLE  . ', ' . CHAMPIONSHIPS_TABLE  . ' c , ' . 
						 CARS_TABLE  . ' ca, ' . EVENTS_TABLE  . ', ' . TRACKS_TABLE   . ' ta, ' .  DIVISIONS_TABLE   . ', ' .  
						 EVENTDATES_TABLE   . ', ' . USERS_TABLE . ' u, ' . DRIVERNAME_TABLE . ' d
				WHERE es_driver = cs_driver AND e_championship = cs_champ AND es_event = e_id AND u.user_id = d.user_id AND
					  es_car = ca.c_id AND ta.t_id = e_track AND es_division = d_id AND cs_div = es_division AND c.c_id = cs_champ ' .
							' AND ed_division = d_id AND ed_event =  e_id AND es_driver = u.user_id AND es_driver = ' . $this->data['user_id'] .
							' ORDER BY c.c_lastRace DESC, ed_date DESC';

		$res = $db->sql_query($q);
		
		$curChamp = -1;
		$events = Array();
		$champ = '';
		$first = true;
		while ($row = $db->sql_fetchrow($res))
		{
			if($curChamp == -1)
			{
				$this->items[] = new addMessage('', $this->data['user_drivername'], true);
				$this->items[] = new renderInclude('tpl/tableStart.tpl');
			}
			if($curChamp != $row['champ_id'])
			{
				if($curChamp > 0)
				{
					$this->items[] = new driver_career($champ, $events, $first);
					$first = false;
				}
				$champ = $row;
				$events = Array();
				$events[] = $row;
				$curChamp = $row['champ_id'];
			}
			else
			{
				$events[] = $row;
			}
		}
		if($curChamp > 0)
		{
			$this->items[] = new driver_career($champ, $events, $first);
			$this->items[] = new renderInclude('tpl/tableEnd.tpl');
		}
		else 
		{
			//nothing to show
			$this->mode = 'information';
			$this->setupInformation();
		}
		
	}
	
	function renderMini()
	{
		global $skin_dir;
		$driver = '<a href="drivers.php?id=' . $this->data['user_id'] . '">' . $this->data['user_drivername'] . '</a> ' . $this->teams; 
		include $skin_dir . 'tpl/driverMini.tpl';
	}
	
	
	function renderInformation()
	{
		global $skin_dir, $lang;
		$teams = $this->teams; 
		$drivername = $this->data['user_drivername'];
		$forumname = $this->data['username'];
		$location = $this->data['user_from'];
		$interests = $this->data['user_interests'];
		$occupation = $this->data['user_occ'];
		$signature = $this->data['user_sig'];
		$contacts = '<a href="' . $this->data['user_pm'] . '">' . $lang['DriverPrivateMessage'] . '</a><br>' .
					'<a href="' . $this->data['user_profile'] . '">' . $lang['DriverForumProfile'] . '</a><br>';
		$website = $this->data['user_website'] == '' ? '' : '<a href="' . $this->data['user_website'] . '">' . $this->data['user_website'] . '</a>';
		$avatar = $this->data['user_avatar'] == '' ? '' : '<img src="' .  $this->data['user_avatar'] . '"</img>';
		include $skin_dir . 'tpl/driverInformation.tpl';
	}
	
	
	function renderStats()
	{
		global $skin_dir, $lang;
		$drivername = $this->data['user_drivername'];
		$avatar = $this->data['user_avatar'] == '' ? '' : '<img src="' .  $this->data['user_avatar'] . '"</img>';
		
		$stats_races = $this->data['stats']['races'];
		$stats_wins = $this->data['stats']['wins'];
		$stats_podiums = $this->data['stats']['podiums'];
		$stats_ptsfinishes = $this->data['stats']['pts_finishes'];
		$stats_laps = $this->data['stats']['race_laps'];
		$stats_lapsled = $this->data['stats']['race_led'];
		$stats_points = $this->data['stats']['points'];
		$stats_poles = $this->data['stats']['pole_positions'];
		$stats_fastestlaps = $this->data['stats']['fastest_laps'];
		
		include $skin_dir . 'tpl/driverStats.tpl';
	}
	
	function renderItems()
	{
		foreach($this->items as $value)
		{
			$value->render();
		}
	}
}

?>